﻿@using Think9.Models;
@model Think9.Models.RuleSingleEntity
@{ ViewBag.Title = "Add";
    Layout = "~/Areas/Shared/_LayuiForm.cshtml"; }

<form class="layui-form" lay-filter="formUser">
    <input style="display:none" name="RuleId" id="RuleId" value="@ViewBag.id">
    <input style="display:none" id="ValueScope" value="@Model.ValueScope">
    <div class="layui-form-item">
        <label class="layui-form-label required">名 称</label>
        <div class="layui-input-block">
            <input type="text" name="Name" placeholder="必填 请输入名称" autocomplete="off" class="layui-input" lay-verify="required">
        </div>
    </div>
    <div class="layui-form-item">
        <label class="layui-form-label required">取值方式</label>
        <div class="layui-input-block">
            <input type="text" name="TbId_Exa" id="TbId_Exa" readonly="readonly" class="layui-input" lay-verify="required">
        </div>
    </div>
    <div class="layui-form-item required">
        <label class="layui-form-label required">Value字段</label>
        <div class="layui-input-block">
            <select id="ValueFiled" name="ValueFiled" lay-verify="required">
                @foreach (valueTextEntity item in ((List
<valueTextEntity>
)ViewBag.SelectSourceList).FindAll(x => x.ClassID.Equals("valuetext")))
                {

                    <option value="@item.Value">@item.Text</option>
                }
            </select>
            <tip>Value字段存储在数据库中</tip>
        </div>
    </div>
    <div class="layui-form-item">
        <label class="layui-form-label required">Text字段</label>
        <div class="layui-input-block">
            <select id="TxtFiled" name="TxtFiled" lay-verify="required">
                @foreach (valueTextEntity item in ((List
<valueTextEntity>
)ViewBag.SelectSourceList).FindAll(x => x.ClassID.Equals("valuetext")))
                {

                    <option value="@item.Value">@item.Text</option>
                }
            </select>
        </div>
    </div>
    <div class="layui-form-item">
        <div class="layui-row">
            <div class="layui-col-md10">
                <label class="layui-form-label required">排序字段</label>
                <div class="layui-input-block">
                    <select id="OrderFiled" name="OrderFiled" lay-verify="required">
                        @foreach (valueTextEntity item in ((List
<valueTextEntity>
)ViewBag.SelectSourceList).FindAll(x => x.ClassID.Equals("order")))
                        {

                            <option value="@item.Value">@item.Text</option>
                        }
                    </select>
                </div>
            </div>
            <div class="layui-col-md2" align="left">
                <div class="layui-input-block">
                    <input type="hidden" name="OrderType" value="1">
                    <input type="checkbox" id="OrderType" name="OrderType" value="2" title="倒序">
                </div>
            </div>
        </div>
    </div>

    <fieldset class="table-fieldset">
        <legend>筛选条件</legend>
        <div class="layui-form-item">
            <label class="layui-form-label">连 接</label>
            <div class="layui-input-inline">
                <select name="linkstr" id="linkstr">
                    <option value=""></option>
                    <option value=" AND "> 并且 ( AND )</option>
                    <option value=" OR "> 或者 ( OR )</option>
                </select>
            </div>
            <label class="layui-form-label">判 断</label>
            <div class="layui-input-inline">
                <select name="judge" id="judge">
                    <option value="=">等于</option>
                    <option value=">">大于</option>
                    <option value=">=">大于等于</option>
                    <option value="<">小于</option>
                    <option value="<=">小于等于</option>
                    <option value="<>">不等于</option>
                    <option value="like">包含</option>
                </select>
            </div>
        </div>
        <div class="layui-form-item">
            <label class="layui-form-label">字 段</label>
            <div class="layui-input-block">
                <select id="ConditionFiled" name="ConditionFiled">
                    @foreach (valueTextEntity item in ((List
<valueTextEntity>
)ViewBag.SelectSourceList).FindAll(x => x.ClassID.Equals("condition")))
                    {

                        <option value="@item.Value">@item.Text</option>
                    }
                </select>
            </div>
        </div>
        <div class="layui-form-item">
            <label class="layui-form-label">值</label>
            <div class="layui-input-inline">
                <input type="text" name="valuekey" id="valuekey" placeholder="输入或选择" class="layui-input">
            </div>
            <div class="layui-inline">
                <button type="button" class="layui-btn layui-btn-primary" id="selectvalue"><i class="fa fa-check"></i>选择系统参数</button>
            </div>
            <div class="layui-inline">
                <button type="button" class="layui-btn " id="addvalue"><i class="fa fa-plus"></i>添加筛选条件</button>
            </div>
        </div>
        <div class="layui-form-item">
            <label class="layui-form-label">筛选条件</label>
            <div class="layui-input-block">
                <textarea class="layui-textarea" id="LimitStr" name="LimitStr"  placeholder='可自定义SQL语句，如字段1=条件1，其中条件可使用系统参数，如字段1=@@sysuserid，其中@@sysuserid表示用户登陆名'></textarea>
            </div>
        </div>
    </fieldset>

    <br />
    <div class="layui-form-item">
        <div class="layui-input-block">
            <button class="layui-btn" lay-submit lay-filter="edit">编辑</button>
        </div>
    </div>
</form>
<script>
    layui.use(["form", "exLayer", "exUtils", "layer"], function () {
        let form = layui.form;
        let exLayer = layui.exLayer;
        let exUtils = layui.exUtils;
        let tableSelect = layui.tableSelect;
        let layer = layui.layer;

        let $ = layui.$;

        form.render(); //初始化表单，要加上，不然刷新部分组件可能会不加载

        getVal();

        //给表单赋值
        form.val("formUser", {
            "Name": "@Model.Name",
            "TbId_Exa": "@Model.TbId_Exa",
            "TbId": "@Model.TbId",
            "ValueFiled": "@Model.ValueFiled",
            "TxtFiled": "@Model.TxtFiled",
            "OrderFiled": "@Model.OrderFiled",
            "LimitStr": "@Html.Raw(@Model.LimitStr)"
        });
       function getVal() {
            if (@Model.OrderType == "2") {
                $('#OrderType').attr("checked", true);
            }
            else {
                $("#OrderType").removeAttr("checked");
            }
        }

        form.on("submit(edit)", function (data) {
            exUtils.ajax("/SysTable/RuleSingle/Edit", "post", { model: data.field, id: $("#RuleId").val()}, true).done(function (response) {
                exLayer.greenTickMsg(response.message, function () {
                    $(window.parent.document).find('#search').click();
                    parent.layer.close(parent.layer.getFrameIndex(window.name));
                });
            }).fail(function (error) {
                console.log(error);
            });
            return false;
        });

        $('#selectvalue').on('click', function () {
            exLayer.openMiddle('选择', '/SysTable/RuleList/PUParameter', '500px', '450px', null, null);
        });

        $('#addvalue').on('click', function () {
            var _old = $("#LimitStr").val();
            var _new = _old + ' ' + $("#linkstr").val() + ' ' + $("#ConditionFiled").val() + ' ' + $("#judge").val() + ' ' + $("#valuekey").val();
            $("#LimitStr").val(_new);

            $("#valuekey").val('');
        });

        function GetSelectValueFieldByTb(id) {
            var controlid = "#ValueFiled";
            $("" + controlid + "").empty(); //清空控件
            $.getJSON("/SysTable/RuleSingle/GetSelectValueFieldByTb", { id: id }, function (data) {
                $.each(data, function (i, item) {
                    $("" + controlid + "").append(new Option(item.Text, item.Value));
                });
                form.render();//一定要加并且只能加在此处 否则下拉框不正确显示
            });
        }

    });
</script>